//
//  UserCenterViewController.m
//  SinaWebo
//
//  Created by wwj on 16/6/27.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "UserCenterViewController.h"
#import "CircleView.h"
#import "MeService.h"
#import "CardGroup.h"
#import "SettingCardCell.h"
#import "UserCenterViewCell.h"
#import "CenterHeaderView.h"
#import "UIBarButtonItem+Extension.h"
#import "CardlistInfo.h"
#import "Toolbarmenu.h"
#import "CenterButtom.h"
#import "Card.h"
#import "Picitem.h"
#import "UserPerfenceUtil.h"
#import <Masonry.h>
#import <MJRefresh.h>
#import <UIButton+WebCache.h>

#define UserCenterPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"UserCenter.data"]

@interface UserCenterViewController ()<CircleViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) CircleView *circleView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet CenterButtom *liftB;
@property (weak, nonatomic) IBOutlet CenterButtom *rightB;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) CardlistInfo *cardListInfo;
@end

@implementation UserCenterViewController
static NSString *const CELL_ID = @"SettingCardCell";
static NSString *const CELL_CENTER_ID = @"UserCenterViewCell";
static NSString *const CENTER_HEADER_ID = @"CenterHeaderView";

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员中心";
    self.tableView.sectionFooterHeight = 0.0;
    self.tableView.sectionHeaderHeight = 10.0;
    self.tableView.contentInset = UIEdgeInsetsMake(164, 0, 0, 0);
    self.cardListInfo = [[CardlistInfo alloc] init];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"image_more_disable" highIcon:@"image_more_highlighted" target:self action:@selector(onClickRightBarItem)];
    
    [self addCircleView];
    
    [self registXib];
    
    [self getUserCenterData];
}

- (void)addCircleView{
    CircleView *circleView = [CircleView circleView];
    circleView.frame = CGRectMake(0, -100, kScreenW, 125);
    circleView.delegate = self;
    [self.tableView addSubview:circleView];
    self.circleView = circleView;
}

- (void)registXib{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SettingCardCell class]) bundle:nil] forCellReuseIdentifier:CELL_ID];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UserCenterViewCell class]) bundle:nil] forCellReuseIdentifier:CELL_CENTER_ID];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CenterHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:CENTER_HEADER_ID];
}

- (void)onClickRightBarItem{
    UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alterVC addAction:[UIAlertAction actionWithTitle:@"续费会员" style:UIAlertActionStyleDefault handler:nil]];
    [alterVC addAction:[UIAlertAction actionWithTitle:@"全部会员特权" style:UIAlertActionStyleDefault handler:nil]];
    [alterVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alterVC animated:YES completion:nil];
}

#pragma mark CircleView 代理
- (void)circleView:(CircleView *)circleView didSelectItemAtIndex:(NSInteger)index{
}

#pragma mark TableView 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Card *card = self.dataArray[section];
    if (card.show_type.length > 0) {
        return 1;
    }
    return card.card_group.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    Card *card = self.dataArray[section];
    if (!card.title || card.title.length == 1) {
        return 0;
    }
    return 30;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Card *card = self.dataArray[indexPath.section];
    if (card.show_type.length > 0) {
        return 160;
    }
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CenterHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CENTER_HEADER_ID];
    Card *card = self.dataArray[section];
    headerView.title = card.title;
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Card *card = self.dataArray[indexPath.section];
    if (card.show_type.length > 0) {
        UserCenterViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_CENTER_ID forIndexPath:indexPath];
        cell.groupArray = card.card_group;
        return cell;
    }else{
        SettingCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath];
        Card *card = _dataArray[indexPath.section];
        cell.model = card.card_group[indexPath.row];
        return cell;
    }
}


- (void)getUserCenterData{
    UserCenterRequest *request = [[UserCenterRequest alloc] init];
    request.uicode = @"10000011";
    request.luicode = @"10000011";
    request.containerid = @"107103000201";
    request.fid = @"107103000201";
    request.lfid = @"1005055551683910";
    request.lfid = @"1005055551683910";
    
    [MeService getUserCenterWithRequest:request success:^(UserCenterResponse *response) {
        if (response) {
            NSMutableArray *dataArray = [response.cards mutableCopy];
            self.cardListInfo = response.cardlistInfo;
            [self setupPageInfo];
            [self setupHeaderData:dataArray.firstObject];
            [dataArray removeObjectAtIndex:0];
            self.dataArray = dataArray;
            [self.tableView reloadData];
        }
    } error:nil];
}

- (void)setupHeaderData:(Card *)card{
    NSMutableArray *images = [NSMutableArray array];
    NSArray *picItems = [card.card_group.firstObject pic_items];
    if (picItems.count) {
        for (Picitem *picItem in picItems) {
            [images addObject:picItem.pic_big];
        }
    }else if ([card.card_group.firstObject pic]){
        [images addObject:[card.card_group.firstObject pic]];
    }
    
    self.circleView.dataArray = images;
}

- (void)setupPageInfo{
    Toolbarmenu *liftM = _cardListInfo.toolbar_menus.firstObject;
    [_liftB setTitle:liftM.name forState:UIControlStateNormal];

    [_liftB sd_setImageWithURL:[NSURL URLWithString:liftM.pic] forState:UIControlStateNormal];
    
    Toolbarmenu *rightM = _cardListInfo.toolbar_menus.lastObject;
    [_rightB setTitle:rightM.name forState:UIControlStateNormal];
    [_rightB sd_setImageWithURL:[NSURL URLWithString:liftM.pic] forState:UIControlStateNormal];
}

@end
